/*
 * Copyright (c) 2005-2007 feyaSoft Corp. All Rights Reserved.
 */
package com.feyaSoft.plugin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.MissingResourceException;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * Date util now...
 * 
 * @author calvin
 */
public class DateUtil {
	private static String defaultDatePattern = "yyyy-MM-dd";

	static {
		try {
			Locale locale = LocaleContextHolder.getLocale();
		} catch (MissingResourceException mse) {
			// do nothing
		}
	}

	public static String getDatePattern() {
		return defaultDatePattern;
	}

	public static String getToday() {
		Date today = new Date();
		return format(today);
	}

	public static String format(Date date) {
		return date == null ? "" : format(date, getDatePattern());
	}

	public static String format(Date date, String pattern) {
		return date == null ? "" : new SimpleDateFormat(pattern).format(date);
	}

	public static Date parse(String strDate) throws ParseException {
		return StringUtils.isBlank(strDate) ? null : parse(strDate,
				getDatePattern());
	}

	/**
	 * parse string with pattern
	 * 
	 * @param strDate
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String strDate, String pattern)
			throws ParseException {
		return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(
				pattern).parse(strDate);
	}

	/**
	 * this will add month to this date
	 * 
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}

	/**
	 * this will add day to the this date
	 * 
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 * Add hour min now...
	 * 
	 * @param date
	 * @param hour
	 * @param min
	 * @return
	 */
	public static Date addHourMin(Date date, int hour, int min) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, hour);
		cal.add(Calendar.MINUTE, min);
		return cal.getTime();
	}

	/**
	 * this just return midnight of date...
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getDMYDate(Date date) {
		Date result = date;
		try {
			date = parse(format(date));
		} catch (Exception e) {
			// do nothing
		}

		return result;
	}

	/**
	 * return sunday of this week and dayOfWeek should be like: Calendar.SUNDAY
	 * 
	 * @param date
	 * @return
	 */
	public static Date getWeekDay(Date date, int dayOfWeek) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
		return cal.getTime();
	}

	/**
	 * This will return Sunday of next week....
	 * 
	 * @param date
	 * @return
	 */
	public static String weekdayOfNextWeek(Date date, int weekday) {
		Date thisWeekDay = DateUtil.getWeekDay(date, weekday);
		Date nextWeekDay = DateUtil.addDay(thisWeekDay, 7);
		return format(nextWeekDay, "EEE dd MMM, yyyy");
	}
	
	/**
	 * This will return Sunday of next week....
	 * 
	 * @param date
	 * @return
	 */
	public static String weekdayOfThisWeek(Date date, int weekday) {
		Date thisWeekDay = DateUtil.getWeekDay(date, weekday);
		return format(thisWeekDay, "EEE dd MMM, yyyy");
	}
	
	/**
	 * return week of day by date
	 * @param date
	 * @return
	 */
	public static int getWeekOfDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);		
		return cal.get(Calendar.DAY_OF_WEEK);
	}
	
	/**
	 * set start end date period...
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int escapeDays(Date startDate, Date endDate) {
		int result = -1;		
		Long mills = endDate.getTime() - startDate.getTime();
		
		try {
			result = (int)(mills/86400000);
		} catch (Exception e) {
			// do nothing
		}
		
		return result;
	}
}